package com.fu.shop.service.Impl;

import com.fu.core.common.BusinessException;
import com.fu.core.common.ExceptionCode;
import com.fu.core.pojo.Admin;
import com.fu.core.pojo.Order;
import com.fu.core.pojo.Shop;
import com.fu.core.util.JsonUtil;
import com.fu.core.util.ThreadLocalUtil;
import com.fu.shop.mapper.AdminMapper;
import com.fu.shop.mapper.IndexMapper;
import com.fu.shop.mapper.OrderMapper;
import com.fu.shop.mapper.ShopMapper;
import com.fu.shop.service.IndexService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author 付龙江
 * @date 2022/2/5 11:19
 */
@Service("indexService")
public class IndexServiceImpl implements IndexService {

    private final static String GOODS_REMARK = "goods_remark:";
    AdminMapper adminMapper;
    ShopMapper shopMapper;
    OrderMapper orderMapper;
    IndexMapper indexMapper;
    RedisTemplate<String,Object> redisTemplate;
    ValueOperations<String,Object>stringOps;
    static Map<String,Object> map;
   @Autowired(required = false)
    public IndexServiceImpl(AdminMapper adminMapper, ShopMapper shopMapper, OrderMapper orderMapper, IndexMapper indexMapper, RedisTemplate<String, Object> redisTemplate) {
        this.adminMapper = adminMapper;
        this.shopMapper = shopMapper;
        this.orderMapper = orderMapper;
        this.indexMapper = indexMapper;
        this.redisTemplate = redisTemplate;
        stringOps = redisTemplate.opsForValue();

    }

    private static void getThreadMessage(){
        Object object = ThreadLocalUtil.getObject();
        System.out.println(object);
        map = JsonUtil.stringToObject(object.toString(), Map.class);
        System.out.println(map);
    }

    @Override
    public Map<String, Object> indexMessage() throws Exception {
        getThreadMessage();
       //map没有获取到线程值 证明用户信息没登录  或者过期了
       if (map == null){
           throw new BusinessException(ExceptionCode.LOGIN_EXPIRED);
       }
       //获取管理员id和店铺的id
        Object o = map.get("shopId");
        Object o1 = map.get("adminId");
        if (StringUtils.isEmpty(o) || StringUtils.isEmpty(o1)){
            throw new BusinessException(ExceptionCode.NO_MATCHING_RESULT);
        }
        Integer shopId = Integer.valueOf(o.toString());
        Integer adminId = Integer.valueOf(o1.toString());
        //利用tk查到对应的信息
        Admin admin = adminMapper.selectByPrimaryKey(adminId);
        Shop shop = shopMapper.selectByPrimaryKey(shopId);
        Map<String,Object> shopMap = new HashMap<>(16);
        shopMap.put("shopLogo",shop.getShopLogo());
        shopMap.put("adminName",admin.getAdminName());
        shopMap.put("adminLoginTime",admin.getAdminLoginTime().toString().substring(0,19));
        shopMap.put("shopName",shop.getShopName());
        //在redis中获取店铺评分
        Set<String> keys = redisTemplate.keys(GOODS_REMARK + "*");
        if (keys!=null && keys.size()>0){
            //计数器
            Double count = 0.0;
            //求和
            Double sum = 0.0;
            for (String key : keys) {
                Object o2 = stringOps.get(key);
                Map map = JsonUtil.stringToObject(String.valueOf(o2), Map.class);
                count++;
                System.out.println(map.get("grade"));
                sum += Double.parseDouble(map.get("grade").toString());
            }
            if (count == 0.0){
                shopMap.put("storeGrade",0.0);
            }else {
                shopMap.put("storeGrade",sum/count);
            }
        }else {
            shopMap.put("storeGrade",0.0);
        }

        //获取商品各项数据
        Map<String,Object> goodsMap = new HashMap<>(16);
        goodsMap.put("saleGoods",indexMapper.selectSaleGoods());
        goodsMap.put("todayShelf",indexMapper.selectTodayShelf());
        goodsMap.put("downShelf",indexMapper.selectDownShelf());
        goodsMap.put("stockNum",indexMapper.selectStockNum());

        //利用tk获取完成了的订单信息
        Example example = new Example(Order.class);
        PageHelper.startPage(1,5);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderStatus",1);
        List<Order> orderList = orderMapper.selectByExample(example);
        PageInfo<Order> of = PageInfo.of(orderList);

        Map<String,Object> map = new HashMap<>(16);
        map.put("shopMap",shopMap);
        map.put("goodsMap",goodsMap);
        map.put("goodsSaleMessage",of.getList());
        map.put("code","200");
        return map;
    }
}
